<?php

namespace App\Http\Controllers\Admin;

use App\Models\LessonPage;

class LessonPageController extends Controller
{
    /*
     * @api get /course-api/a/lessons/{lessonId}/pages 课时页面列表
     * @version 0.0.1
     * @group admin
     *
     * @param query int [type] 类型，1绘本，2精读
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{
     *    "items":[]
     * }
     * }
     */
    public function index($lessonId)
    {
        $type = intval($this->request->input("type", 1));
        $query = LessonPage::ofType($type);
        $pages = $query->where('lesson_id', $lessonId)->orderBy('sequence', 'asc')->orderBy('id', 'asc')
            ->get(['id', 'name']);
        $items = [];
        foreach ($pages as $page) {
            $items[] = [
                'id' => $page->id,
                'name' => $page->getName(),
            ];
        }
        return $this->sendSuccessJson(['items'=>$items]);
    }

    /*
     * @api post /course-api/a/lessons/{lessonId}/pages 新增课时页面
     * @version 0.0.1
     * @group admin
     *
     * @param body int type 类型，1读绘本，2精读
     * @param body array content 内容
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":"id"
     * }
     */
    public function store($lessonId)
    {
        $this->validate($this->request, [
            'type' => 'required|integer',
            'name' => 'string',
            'content' => 'array'
        ]);
        $type = $this->request->input('type');
        $lessonPageObj = new LessonPage();
        $lessonPageObj->setLessonId($lessonId);
        $lessonPageObj->setType($type);
        if ($this->request->has('name')) {
            $lessonPageObj->setName($this->request->input('name'));
        }
        if ($this->request->has('content')) {
            $lessonPageObj->setContent($this->request->input('content'));
        }
        $lessonPageObj->save();
        LessonPage::parseKnowledge($lessonId);
        return $this->sendSuccessJson($lessonPageObj->id);
    }

    /*
     * @api put /course-api/a/lessons/{lessonId}/pages/{pageId} 更新page
     * @version 0.0.1
     * @group admin
     *
     * @param path int lessonId 课时ID
     * @param path int pageId 课时页面ID
     * @param body array content 内容
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{
     *    "items":[]
     * }
     * }
     */
    public function update($lessonId, $pageId)
    {
        $this->request->merge(['id'=>$pageId]);
        $this->validate($this->request, [
            'id' => 'required|integer',
            'name' => 'string',
            'content' => 'array',
        ]);
        /** @var LessonPage $lessonPageObj */
        $lessonPageObj = LessonPage::find($pageId);
        if ($this->request->has('name')) {
            $lessonPageObj->setName($this->request->input('name'));
        }
        if ($this->request->has('content')) {
            $lessonPageObj->setContent($this->request->input('content'));
        }
        $lessonPageObj->save();
        LessonPage::parseKnowledge($lessonId);
        return $this->sendSuccessJson($lessonPageObj->id);
    }

    /*
     * @api put /course-api/a/lessons/{lessonId}/pages-sequence page排序
     * @version 0.0.1
     * @group admin
     *
     * @param path int lessonId 课时ID
     * @param path int pageIds 课时页面ID
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{
     *    "items":[]
     * }
     * }
     */
    public function sequence($lessonId)
    {
        $this->request->merge(['lessonId'=>$lessonId]);
        $this->validate($this->request, [
            'lessonId' => 'required|integer',
            'pageIds' => 'array',
        ]);
        $pageIds = $this->request->input('pageIds');
        foreach ($pageIds as $index => $pageId) {
            /** @var LessonPage $lessonPageObj */
            $lessonPageObj = LessonPage::find($pageId);
            $lessonPageObj->setSequence($index);
            $lessonPageObj->save();
        }
        return $this->sendSuccessJson();
    }

    /*
     * @api get /course-api/a/lessons/{lessonId}/pages/{pageId} page详情
     * @version 0.0.1
     * @group admin
     *
     * @param path int lessonId 课时ID
     * @param path int pageId 课时页面ID
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{
     *    "items":[]
     * }
     * }
     */
    public function show($lessonId, $pageId)
    {
        $this->request->merge(['lessonId'=>$lessonId, 'pageId'=>$pageId]);
        $this->validate($this->request, [
            'lessonId' => 'required|integer',
            'pageId' => 'required|integer',
        ]);
        /** @var LessonPage $lessonPageObj */
        $lessonPageObj = LessonPage::find($pageId);
        $data = [
            'id' => $lessonPageObj->id,
            'name' => $lessonPageObj->getName(),
            'content' => $lessonPageObj->getContent(),
        ];
        return $this->sendSuccessJson($data);
    }

    /*
     * @api delete /course-api/a/lessons/{lessonId}/pages/{pageId} 删除page
     * @version 0.0.1
     * @group admin
     *
     * @param path int knowledgeId 知识点ID
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{
     *    "items":[]
     * }
     * }
     */
    public function destroy($lessonId, $pageId)
    {
        LessonPage::destroy(intval($pageId));
        return $this->sendSuccessJson();
    }
}
